;(function ($, window, undefined) {
  'use strict';

  var $doc = $(document),
      Modernizr = window.Modernizr;

  $(document).ready(function() {
    $.fn.foundationAlerts           ? $doc.foundationAlerts() : null;
    $.fn.foundationButtons          ? $doc.foundationButtons() : null;
    $.fn.foundationAccordion        ? $doc.foundationAccordion() : null;
    $.fn.foundationNavigation       ? $doc.foundationNavigation() : null;
    $.fn.foundationTopBar           ? $doc.foundationTopBar() : null;
    $.fn.foundationCustomForms      ? $doc.foundationCustomForms() : null;
    $.fn.foundationMediaQueryViewer ? $doc.foundationMediaQueryViewer() : null;
    $.fn.foundationTabs             ? $doc.foundationTabs({callback : $.foundation.customForms.appendCustomMarkup}) : null;
    $.fn.foundationTooltips         ? $doc.foundationTooltips() : null;
    $.fn.foundationMagellan         ? $doc.foundationMagellan() : null;
    $.fn.foundationClearing         ? $doc.foundationClearing() : null;

    $.fn.placeholder                ? $('input, textarea').placeholder() : null;
  });

  // UNCOMMENT THE LINE YOU WANT BELOW IF YOU WANT IE8 SUPPORT AND ARE USING .block-grids
  // $('.block-grid.two-up>li:nth-child(2n+1)').css({clear: 'both'});
  // $('.block-grid.three-up>li:nth-child(3n+1)').css({clear: 'both'});
  // $('.block-grid.four-up>li:nth-child(4n+1)').css({clear: 'both'});
  // $('.block-grid.five-up>li:nth-child(5n+1)').css({clear: 'both'});

  // Hide address bar on mobile devices (except if #hash present, so we don't mess up deep linking).
  if (Modernizr.touch && !window.location.hash) {
    $(window).load(function () {
      setTimeout(function () {
        window.scrollTo(0, 1);
      }, 0);
    });
  }

})(jQuery, this);



/* SCM Tools
*******************************************************************************/

var scm = {}


scm.Dialog = function(content) {
    var self = this;

    this.id = (new Date()).getTime();

    this.fog = $('<div class="dialog-fog"></div>').click(function() {
        self.close();
    })
    
    if (!$("html").find(".dialog-fog").length)
        this.fog.appendTo("html");

    this.dialog = $('<div class="dialog"></div>').appendTo("html").append(
        $('<span class="dialog-close"></span>').click(function() {
            self.close();
        }),
        content,
        $('<button class="button dialog-button">' + 'OK' + '</button>').click(function() {
            self.close();
        })
    )

    this.dialog.css({
        top: ($(document).height() - this.dialog.outerHeight()) / 2 * .7,
        left: ($(document).width() - this.dialog.outerWidth()) / 2
    });

    this.dialog.hide().fadeIn(200);

    $(window).on("keydown." + this.id, function(event) {
        if (event.keyCode == 27)
            self.close();
    })

}

scm.Dialog.prototype.close = function() {
    $(window).off("." + this.id);
    
    this.fog.remove();
    this.dialog.fadeOut(200, function() {
        $(this).remove();
    });

    delete this;
}
